/**
  ******************************************************************************
  * @file           command.c
  * @author         ZohyCao
  * @brief          shell命令注册集
  *                 适用于机器人队电控培训,须配合shell命令解释器
  * 使用步骤：
  *         略
  ******************************************************************************
  *
  * COPYRIGHT(c) 2019 ZohyCao
  *
  ******************************************************************************
  */
/* Includes ---------------------------------------------------*/
#include "main.h"
#include "can.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"
#include "stdio.h"
#include "stdarg.h"
#include "string.h"
#include "stdlib.h"
#include "shell.h"
#include "command.h"
#include "motor.h"
#include "flash.h"

/* Private types ------------------------------------------------------------*/


/* Private variables --------------------------------------------------------*/

int ID=0;

/* Private function prototypes ----------------------------------------------*/

/* 状态输出 */
void Status(void *arg){
  Uart_Printf("\r\nStatus  for STM32F1.TEAM2:\r\n");
  Uart_Printf("\r\nID=%d\r\n",ID);
  Uart_Printf("\r\nTIM1:Timer\r\n");
  Uart_Printf("   PSC:%d  ARR:%d  T:%.3fs\r\n",TIM1->PSC+1,TIM1->ARR+1,(TIM1->ARR)*(TIM1->PSC)/72000000.0);
  Uart_Printf("\r\nTIM2:Encoder Mode\r\n");
  Uart_Printf("\r\nTIM3:PWM\r\n");
  Uart_Printf("   3.CH1: PSC:%d  ARR:%d  Frequency:%dHz  +Duty:%.2f%%\r\n",TIM3->PSC+1,TIM3->ARR+1,72000000/((TIM3->ARR)*(TIM3->PSC)),100.0*TIM3->CCR1/(TIM3->ARR+1)); 
  Uart_Printf("   3.CH2: PSC:%d  ARR:%d  Frequency:%dHz  +Duty:%.2f%%\r\n",TIM3->PSC+1,TIM3->ARR+1,72000000/((TIM3->ARR)*(TIM3->PSC)),100.0*TIM3->CCR2/(TIM3->ARR+1));
  Uart_Printf("   3.CH3: PSC:%d  ARR:%d  Frequency:%dHz  +Duty:%.2f%%\r\n",TIM3->PSC+1,TIM3->ARR+1,72000000/((TIM3->ARR)*(TIM3->PSC)),100.0*TIM3->CCR3/(TIM3->ARR+1));
  Uart_Printf("   3.CH4: PSC:%d  ARR:%d  Frequency:%dHz  +Duty:%.2f%%\r\n",TIM3->PSC+1,TIM3->ARR+1,72000000/((TIM3->ARR)*(TIM3->PSC)),100.0*TIM3->CCR4/(TIM3->ARR+1));
  Uart_Printf("\r\nTIM4:PWM\r\n");
  Uart_Printf("   4.CH2: PSC:%d  ARR:%d  Frequency:%dHz  +Duty:%.2f%%\r\n",TIM4->PSC+1,TIM4->ARR+1,72000000/((TIM4->ARR+1)*(TIM4->PSC+1)),100.0*TIM4->CCR1/(TIM3->ARR+1));
  Uart_Printf("\r\nShell Ready >");
}


/* 电机can指令 */
/* 参数依次为id号、指令类型 */
void Can_Send_ToMotor(void *arg){
    char *argv[4];
    int argc = cmdline_strtok((char*)arg,argv,4);
    if(argc == 4){
        can_msg msg;
        msg.in[0] = atoi(argv[2]);
        msg.in[1] = atoi(argv[3]);
        Can_TxMessage(0,atoi(argv[1]),msg.ui8,8);
    }
    else Uart_Printf("Error");
    Uart_Printf("Shell Ready >");
}


/* CAN总线数据发送 */
/* 参数依次为：命令、帧头、ID、数据*/
void Can_Send(void *arg){
    char *argv[4];
    int argc = cmdline_strtok((char*)arg,argv,4);
    if(argc==4){
          Can_TxMessage(atoi(argv[1]),strtol(argv[2],NULL,16),argv[3],strlen(argv[3]));//CAN发送
    }
    else  Uart_Printf("\r\nERROR\r\nShell Ready >");
    
}

/* 更改TIM3的频率、占空比 */
/* 参数依次为：命令、类型（f/q）、数值 */
/* f:频率，单位为Hz */
/* q:占空比，0~1，须输入小数 */
void TIM3_Control(void *arg){
    char *argv[4];
    int argc = cmdline_strtok((char*)arg,argv,4);
    if(argc==4){
      if(!strcmp(argv[2],"f")){
              //float q = (float)(TIM3->CCR1)/((TIM3->ARR)+1);
              TIM3 ->ARR = (1000/atoi(argv[3]))*100-1;
              flash_data[0] = TIM3 ->ARR;
              //TIM3 ->CCR1 = q * ((TIM3 ->ARR)+1)-1;
              Uart_Printf("TIM3_PWM Frequency Changed to %.2f",atof(argv[3]));
          }
      else if(!strcmp(argv[2],"q")){
        switch(atoi(argv[1])){
      case 1:{
              TIM3 ->CCR1 = atof(argv[3])*((TIM3 ->ARR)+1);
              flash_data[1] = TIM3 ->CCR1;
              Uart_Printf("TIM3_PWM_CH1 +Duty Changed to %.2f",atof(argv[3]));       
              break;}
      case 2:{
              TIM3 ->CCR2 = atof(argv[3])*((TIM3 ->ARR)+1);
              Uart_Printf("TIM3_PWM_CH2 +Duty Changed to %.2f",atof(argv[3]));
              break;}
      case 3:{
              TIM3 ->CCR3 = atof(argv[3])*((TIM3 ->ARR)+1);
              Uart_Printf("TIM3_PWM_CH3 +Duty Changed to %.2f",atof(argv[3]));
              break;}
      case 4:{
              TIM3 ->CCR4 = atof(argv[3])*((TIM3 ->ARR)+1);
              Uart_Printf("TIM3_PWM_CH4 +Duty Changed to %.2f",atof(argv[3]));  
              break;}
      default:Uart_Printf("Wrong Parameter!");
      }
     }
     write_prams();
    }
    else Uart_Printf("Wrong Command!"); 
    Uart_Printf("\r\nSTM32F1 >");
}


/* 命令注册初始化 */
void Command_Init(void){

  shell_register_command("tim3",TIM3_Control);
  shell_register_command("can",Can_Send);
  shell_register_command("canmotor",Can_Send_ToMotor);
  shell_register_command("status",Status);
}





